package blog.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import blog.entity.Article;
import blog.entity.Comment;
import blog.mapper.ArticleMapper;
import blog.mapper.CommentMapper;
import blog.service.CommentService;

@Service
public class CommentServiceImpl implements CommentService{

	@Resource
	private  CommentMapper commentMapper;
	
	@Resource
	private ArticleMapper articleMapper;
	
	public List<Comment> listRecentComment(Integer n) {
		List <Comment> commentList=commentMapper.listRecentComment(n);
		for(Comment c:commentList) {	
			Article article =articleMapper.getArticleById(c.getCommentArticleId());
			c.setArticle(article);
		}
		return  commentList;
	}

	@Override
	public PageInfo<Comment> getPageCommentList(Integer pageIndex, Integer pageSize) {
		// 进行分页
		PageHelper.startPage(pageIndex, pageSize);
		List<Comment> commentList=commentMapper.findAll();
		for (Comment i:commentList) {
			i.setArticle(articleMapper.getArticleById(i.getCommentArticleId()));
		}
		// 包装查出来的列表
		return new PageInfo<Comment>(commentList);
	}

	@Override
	public Comment getCommentById(Integer commentId) {
		return commentMapper.getCommentById(commentId);
	}

	@Override
	public void add(Comment comment) {
		commentMapper.add(comment);
		
	}		

}
